package com.cattsoft.qr.report.authorization;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.cattsoft.pub.util.JdbcUtil;
import com.cattsoft.qr.report.dao.DataAccessTools;
import com.cattsoft.qr.report.tools.StringTools;

public class Authorization
{
	public static String getUserLocalnetValues(String loginID) throws Exception
	{
		if (StringTools.isStringNullOrEmptyValue(loginID))
		{
			return null;
		}
		
		String values = null;
		Statement st = null;
		ResultSet rs = null;	
		try
		{
			String sql = "select * from v_rpt_auth_local_net where login_id = " + loginID;		
			Connection conn = DataAccessTools.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next())
			{
				String localnetID = rs.getString("local_net_id");
				if (!StringTools.isStringNullOrEmptyValue(localnetID))
				{
					if (null == values)
					{
						values = localnetID;
					}
					else
					{
						values += ", " + localnetID;
					}
				}
			}
		}
		finally
		{
			JdbcUtil.close(rs, st);
		}
		
		return values;
	}
	
	public static String getUserAreaValues(String loginID, String localnetID) throws Exception
	{
		if (StringTools.isStringNullOrEmptyValue(loginID))
		{
			return null;
		}
		if (StringTools.isStringNullOrEmptyValue(localnetID))
		{
			return null;
		}
		
		String values = null;
		Statement st = null;
		ResultSet rs = null;	
		try
		{
			String sql = "select * from v_rpt_auth_area where login_id = " + loginID + " and local_net_id = " + localnetID;		
			Connection conn = DataAccessTools.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while(rs.next())
			{
				String areaID = rs.getString("area_id");
				if (!StringTools.isStringNullOrEmptyValue(areaID))
				{
					if (null == values)
					{
						values = areaID;
					}
					else
					{
						values += ", " + areaID;
					}
				}
			}
		}
		finally
		{
			JdbcUtil.close(rs, st);
		}
		
		return values;
	}
}
